import 'package:date_format/date_format.dart';
import 'package:flutter/material.dart';
import 'package:s_mankey/config/resource_mananger.dart';
import 'package:s_mankey/config/router_config.dart';
import 'package:s_mankey/model/video_list_model.dart';
import 'package:s_mankey/service/smonkey_repository.dart';
import 'package:s_mankey/ui/widget/error_widget.dart';
import 'package:s_mankey/ui/widget/loading_widget.dart';
import 'package:s_mankey/ui/widget/unlogin_nav.dart';

class MyVideos extends StatefulWidget {

  MyVideos({Key key}) : super(key: key);


  @override
  _MyVideos createState() => _MyVideos();

}

class _MyVideos extends State<MyVideos> {
  List<VideoListModel> datas;

  @override
  void initState() {
    UnLoginNav(context).unLogin();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 1,
        title: Text('我的彩析'),
      ),
      body: FutureBuilder(future: SmonkeysRepository.fetchVideoList(),builder:
          (BuildContext context, AsyncSnapshot snapshot) {

        /// 请求中
        if (snapshot.connectionState != ConnectionState.done) {
          return LoadingWidget();
        }
        /// 请求结束
        if (snapshot.hasError) {
          // 请求失败，显示错误
//          Navigator.pushNamed(context, RouterName.loginPage);
          return unLoginErrorTip(errorTip: snapshot.error.toString(), context: context,);
        }

        datas = snapshot.data;

        // 请求成功，显示数据
        return (datas.length <= 0)?  Text('') :
        ListView.builder(
            itemCount: datas.length,
            itemBuilder: (BuildContext context, int index) {
              return GestureDetector(
                behavior: HitTestBehavior.opaque,
                onTap: (){
                  Navigator.pushNamed(context, RouterName.videosDetail, arguments: {"vid": datas[index].videoId});
                },child: Container(
                padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
                child: Column(
                  children: [
                    Padding(
                      padding: const EdgeInsets.all(5),
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          ClipRRect(
                            borderRadius: BorderRadius.circular(5),
                            child: Container(
                              width: 118,
                              height: 66,
                              child: Stack(
                                children: [
                                  Image.network(datas[index].img),
                                  Container(
                                    decoration: BoxDecoration(
                                        color: Color.fromRGBO(47, 47, 47, 0.5)),
                                  ),
                                  Align(
                                      alignment: Alignment(0, 0),
                                      child: Icon(
                                        Icons.video_library,
                                        color: Colors.white,
                                      )),
                                ],
                              ),
                            ),
                          ),
                          Expanded(
                            child: Container(
                              height: 70,
                              child: Column(
                                children: [
                                  Expanded(
                                    child: Container(
                                      padding: EdgeInsets.fromLTRB(10, 5, 10, 0),
                                      alignment: Alignment(-1, -1),
                                      child: Text(datas[index].videoCode),
                                    ),
                                  ),
                                  Container(
                                    padding: EdgeInsets.fromLTRB(0, 0, 10, 5),
                                    alignment: Alignment(1, 1),
                                    child: Text(
                                      '时长: ${datas[index].urlTime}',
                                      style: TextStyle(color: Color(0xffb7b7b7),fontSize: AppFontSize.Size14),
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                    Container(
                      width: AppCustomSize.ScreentWidght,
                      height: 2,
                      decoration: BoxDecoration(
                          color: Color.fromRGBO(201, 201, 201, 0.5)),
                    ),
                  ],
                ),
              ),);
            });
      }),
    );
  }

}